using System;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

using ipi.MCC.Controls;
using ipi.MCC.Global;

namespace ipi.MCC.Pages
{
    public class MCCTreeSitesControl : SitesTreeControl
	{
        protected string sCurrentMaster;
        
        public override void FillTree()
        {
            try
            {
                sCurrentMaster = GetMasterSite(SPContext.Current.Site);
                base.FillTree();
                trvSitesToSearch.ExpandAll();
            }
            catch (Exception exc)
            {
                validGlobal.ErrorMessage = Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Exception);
                validGlobal.IsValid = false;
            }
        }
        
        protected override void CheckNode(TreeNode tnCurrent)
        {
            string sValue = tnCurrent.Value;
            if (sValue == SPContext.Current.Site.ID.ToString())
            {
                tnCurrent.Checked = true;
                tnCurrent.ShowCheckBox = false;
            }
            else
            {
                tnCurrent.Checked = lCheckedItems.Contains(sValue);
            }
        }

        protected override bool AddItem(object spsCurrent)
        {
            bool bReturn = true;

            if (spsCurrent is SPSite)
            {
                bReturn = AddItemSite((SPSite)spsCurrent);
            }

            return bReturn;
        }

        
        protected bool AddItemSite(SPSite spsCurrent)
        {
            bool bAddItem = true;
            
            if (spsCurrent.ServerRelativeUrl == "/ssp/admin")
            {
                bAddItem = false;
            }
            else
            {
                if (this.sCurrentMaster != GetMasterSite(spsCurrent))
                {
                    bAddItem = false;
                }

            }

            return bAddItem;
        }       

        private string GetMasterSite(SPSite spsCurrent)
        {
            string sReturn = spsCurrent.Url;

            try
            {
                WebSettings wsCurrent = new WebSettings(spsCurrent.RootWeb);
                if(!string.IsNullOrEmpty(wsCurrent.MasterWebURL))
                {
                    sReturn = wsCurrent.MasterWebURL;
                }
            }
            catch (Exception exc)
            {
                validGlobal.ErrorMessage = Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Exception);
                validGlobal.IsValid = false;
            }

            return sReturn;
        }
	}
}